<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientView_login extends KBClientView_common
{
	
	var $login_msg = array('comment' => 'need_to_login_comment',
	                       'contact' => 'need_to_login_contact',
					       'enter' => 'need_to_login_enter',
					       'entry' => 'need_to_login_entry',
					       'category' => 'need_to_login_category',
						   'getfile' => 'need_to_login_file',
						   'authtime' => 'need_to_login_authtime');
	
	
	function &execute(&$manager) {
		
		$file = $this->getMsgFile('user_msg.ini', false);
		$this->msg = array_merge($this->msg, AppMsg::parseMsgs($file));
		
		$this->home_link = true;
		$this->meta_title = $this->msg['login_msg'];
		$this->category_nav_generate = false; // not to generate categories in navigation line
		
		$data = $this->getTop($manager, $this->msg['login_msg'], false);
		$data .= $this->getForm($manager);
		
		return $data;
	}
	

	function getForm($manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'login_form.html');
		
		$tpl->tplAssign('action_link', $this->_getLink('all'));
		$tpl->tplAssign('forgot_password_link', $this->_getLink('password'));
		$tpl->tplAssign('register_link', $this->_getLink('register'));
		
		
/*
		$view = array_search($this->msg_id, $this->login_msg);		
		if($view == 'getfile') {
			$cancel_link = $this->_getLink('files');
		} elseif($view == 'entry'){
			$cancel_link = $this->_getLink($view);	
		} else {
			$cancel_link = $this->_getLink($view, $this->category_id, $this->entry_id);
		}
		
		$tpl->tplAssign('cancel_link', $cancel_link);
*/
		
		
		$tpl->tplAssign('user_msg', $this->getErrors());
		
		if($manager->getSetting('register_policy')) {
			$tpl->tplSetNeeded('/register_link');
		}
		
		if(!$manager->getSetting('kb_register_access')) {
			$tpl->tplSetNeeded('/cancel_button');
		}		
		
		
		$tpl->tplParse(array_merge($this->msg, $this->getFormData()));
		return $tpl->tplPrint(1);
	}
	
	
	function validate($values, $manager) {
		
		require_once 'eleontev/Validator.php';
		
		$v =& new Validator($values, false);
		$v->requiredArray('required_msg', array('username', 'password'));
		if($v->getErrors()) {
			return $v->getErrors();
		}
		
		
		$auth = &new AuthPriv();
		$auth->tbl->table = $manager->tbl->user;
		
		$remember = (isset($values['remember']));
		$ret = $auth->doAuth($values['username'], $values['password'], $remember);
		
		if($ret == false) {
			$v->setError('wrong_login_info_msg', 1);			
		}
		
		return $v->getErrors();
	}
}
?>